import { nanoid } from "nanoid"

export default class Process{
    processId
    appId
    isOpen
    createTime
    isLoading
    processMemory
    window
    constructor(appId = null){
        // 初始化进程Id
        this.processId = nanoid().replace(/[0-9-]/g).replace(/undefined/g,'')
        // 初始化进程创建时间
        this.createTime = new Date()
        // 默认加载状态
        this.isLoading = false
        // 初始化进程的窗口状态
        this.isOpen = false
        // 初始化进程对应的appId
        if(!appId) console.error('please bind appId for process')
        this.appId = appId
    }
    // 放置进程数据
    putMeta(key,val){
        this.processMemory.set(key,val)
    }
    getMeta(key){
        return this.processMemory.get(key)
    }
    // 设置内存空间
    setMemory(memory){
        this.processMemory = memory
    }
    closeWindow(){
        this.isOpen = false
    }
    openWindow(){
        this.isOpen = true
    }
    // 获得进程存活时间
    getAliveTime(){
        return new Date()-this.createTime
    }
    // 使进程进入加载状态
    loading(){
        this.isLoading = true
    }
    endLoading(){
        this.isLoading = false
    }
    getId(){
        return this.processId
    }
    setWindow(win){
        this.window = win
    }
    getWindow(){
        return this.window
    }
    getAppId(){
    return this.appId
    }
}